package com.kky.pp.common

import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import com.kky.pp.popup.MemberBuyPopup
import com.kky.pp.entity.MemberSubscribeEntity
import io.rong.imkit.event.EnumEventTag
import com.custom.base.config.BaseConfig
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.base.SDEventManager
import io.rong.imkit.SpName
import io.rong.imkit.entity.OrderCreateEntity
import io.rong.imkit.event.FirebaseEventTag
import com.kky.pp.callback.EmptySuccessCallBack
import com.kky.pp.pay.GooglePayUtils
import io.rong.imkit.utils.FirebaseEventUtils
import razerdp.basepopup.BasePopupWindow

object MemberPopupShow {
    private var memberBuyDialog: MemberBuyPopup?=null

    /**
     *  type: if (direction.toString() == "Right") 1 else 2
     */
    fun memberBuyShow(type:Int?,context:Context){
        if (memberBuyDialog !=null){
            return
        }
        FirebaseEventUtils.logEvent(if (type == 1) FirebaseEventTag.Home_Like_Sub.name else FirebaseEventTag.Home_View_Sub.name)
        memberBuyDialog = MemberBuyPopup(
            context,
            0,
            object : MemberBuyPopup.ChangeMembershipListener {
                override fun onListener(bean: MemberSubscribeEntity.Data.ProductDescriptions) {
                    OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
                        override fun addBody(requestBody: OkHttpBodyEntity) {
                            requestBody.setPost(Constant.user_create_order_url)
                            requestBody.add("productCode", bean.productCode)
                            requestBody.add("productCategory", 1)
                        }

                    }, object : SDOkHttpResoutCallBack<OrderCreateEntity>() {
                        override fun onSuccess(entity: OrderCreateEntity) {
                            GooglePayUtils.instance.start(entity,context,object : EmptySuccessCallBack {
                                override fun OnSuccessListener() {
                                    FirebaseEventUtils.logEvent(if (type == 1) FirebaseEventTag.Home_Like_Subsuccess.name else FirebaseEventTag.Home_View_Subsuccess.name)
                                    BaseConfig.getInstance.setBoolean(SpName.isMember, true)

                                }

                            })
                        }
                    })
                }

                @RequiresApi(Build.VERSION_CODES.O)
                override fun closeListener(refreshTime:Long) {
                    if (type==2){
                        SDEventManager.post(refreshTime,EnumEventTag.INDEX_COUNTDOWN_SHOW.ordinal)
                    }
                }
            })
        memberBuyDialog?.setOnDismissListener(object : BasePopupWindow.OnDismissListener(){
            override fun onDismiss() {
                memberBuyDialog =null
            }

        })
    }
}